<?php
/**
 * CK框架 数组转换为XML。
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 * @author    wujibing<283109896@qq.com>
 * @version   2.0
 */
class arrayToXml
{
	/**
	 * 添加要获得XML的Array数据
	 * @param unknown_type $array
	 */
	public static function addData ($array)
	{
		header('Content-Type:application/xml');
		$str = '<?xml version="1.0" encoding="utf-8" ?>';
		$str .= self::make('root', $array);
		return $str;
	}
	/**
	 * 生成XML
	 * @param string $name
	 * @param mixed $value
	 * @return string
	 */
	private static function make ($name, $value)
	{
		$name = is_int($name) ? 'item':$name;
		$str = '<' . $name . '>';
		if (is_array($value))
		{
			foreach ($value as $xmlKey => $xmlValue)
			{
				$str .= self::make($xmlKey, $xmlValue);
			}
		} else
		{
			$str .= $value;
		}
		$str .= '</' . $name . '>';
		return $str;
	}
}
